import observe from './observe';
import Dep from './Dep';

export default function defineProperty(data, key, val) {
    const dep = new Dep();

    if (val === undefined) {
        val = data[key];
    }

    let childObj = observe(val);

    Object.defineProperty(data, key, {
        enumerable: true,
        configurable: true,
        get() {
            if (Dep.target) {
                dep.depend();
                // data[key].__ob__.dep.depend();
                if (childObj) {
                    childObj.dep.depend();
                }
            }
            return val;
        },
        set(newValue) {
            if (newValue === val) {
                console.log(`你试图修改${key}属性`);
                return ;
            }

            val = newValue;

            childObj = observe(val);

            dep.notify();
        }
    });
}